package com.portal.admin.utils.session;

import com.portal.admin.base.constant.ClientAppConst;
import com.portal.admin.base.constant.AdminAppConst;
import com.portal.admin.base.constant.WxAppConst;
import com.portal.admin.base.entity.UserToken;
import com.portal.admin.base.enums.SystemType;
import com.portal.admin.userAuthor.entity.UserAuthor;
import com.portal.admin.utils.admin.ObjectUtils;
import com.portal.admin.utils.weixni.AdminApiUtils;
import com.portal.admin.utils.weixni.ClientApiUtils;
import com.portal.module.userRegister.entity.UserRegister;
import net.sf.json.JSONObject;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 微信session信息获取
 */
public class SessionUtils {


    /**
     * 设置token
     * @return
     */
    public static JSONObject getJscode2session(String code, int form) {
        setSystemType(form);
        JSONObject result = null;
        if (SystemType.ADMIN.getValue() == getSystemType()) {
            result = ClientApiUtils.getJscode2session(code);
        } else if (SystemType.CLIENT.getValue() == getSystemType()) {
            result = AdminApiUtils.getJscode2session(code);
        } else if (SystemType.WEIXIN.getValue() == getSystemType()) {
            result = ClientApiUtils.getJscode2session(code);
            //String ua = request.getHeader("user-agent").toLowerCase();
            //boolean isWeixin = ua.indexOf("micromessenger") > 0;
        }
        return result;
    }

    /**
     * 设置token
     * @return
     */
    public static void setToken(UserToken token) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        int systemType = getSystemType();
        if (SystemType.ADMIN.getValue() == getSystemType()) {
            request.getSession().setAttribute(AdminAppConst.TOKEN, token);
        } else if (SystemType.CLIENT.getValue() == systemType) {
            request.getSession().setAttribute(ClientAppConst.TOKEN, token);
        } else if (SystemType.WEIXIN.getValue() == systemType) {
            request.getSession().setAttribute(WxAppConst.TOKEN, token);
        }
    }
    /**
     * 获取token
     * @return
     */
    public static UserToken getToken(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        UserToken userToken = null;
        int systemType = getSystemType();
        if (SystemType.ADMIN.getValue() == getSystemType()) {
            userToken = (UserToken) request.getSession().getAttribute(AdminAppConst.TOKEN);
        } else if (SystemType.CLIENT.getValue() == systemType) {
            userToken = (UserToken) request.getSession().getAttribute(ClientAppConst.TOKEN);
        } else if (SystemType.WEIXIN.getValue() == systemType) {
            userToken = (UserToken) request.getSession().getAttribute(WxAppConst.TOKEN);
        }
        return userToken;
    }

    /**
     * 当前用户id
     * @return
     */
    public static String getCurrentUserId(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String userId = null;
        int systemType = getSystemType();
        if (SystemType.ADMIN.getValue() == getSystemType()) {
            userId = (String) request.getSession().getAttribute(AdminAppConst.USER_ID);
        } else if (SystemType.CLIENT.getValue() == systemType) {
            userId = (String) request.getSession().getAttribute(ClientAppConst.USER_ID);
        } else if (SystemType.WEIXIN.getValue() == systemType) {
            userId = (String) request.getSession().getAttribute(WxAppConst.USER_ID);
        }
        return userId;
    }

    /**
     * 设置注册信息
     * @return
     */
    public static void setUserInfo(UserRegister userRegister) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        int systemType = getSystemType();
        if (SystemType.ADMIN.getValue() == getSystemType()) {
            request.getSession().setAttribute(AdminAppConst.USER_ID, userRegister.getId());
            request.getSession().setAttribute(AdminAppConst.USER_INFO, userRegister);
        } else if (SystemType.CLIENT.getValue() == systemType) {
            request.getSession().setAttribute(ClientAppConst.USER_ID, userRegister.getId());
            request.getSession().setAttribute(ClientAppConst.USER_INFO, userRegister);
        } else if (SystemType.WEIXIN.getValue() == systemType) {
            request.getSession().setAttribute(WxAppConst.USER_ID, userRegister.getId());
            request.getSession().setAttribute(WxAppConst.USER_INFO, userRegister);
        }
    }
    /**
     * 设置授权信息
     * @return
     */
    public static void setUserAuthor(UserAuthor userAuthor) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        int systemType = getSystemType();
        if (SystemType.ADMIN.getValue() == getSystemType()) {
            request.getSession().setAttribute(AdminAppConst.USER_AUTHOR, userAuthor);
        } else if (SystemType.CLIENT.getValue() == systemType) {
            request.getSession().setAttribute(ClientAppConst.USER_AUTHOR, userAuthor);
        } else if (SystemType.WEIXIN.getValue() == systemType) {
            request.getSession().setAttribute(WxAppConst.USER_AUTHOR, userAuthor);
        }
    }

    /**
     * 获取授权信息
     * @return
     */
    public static UserAuthor getUserAuthor() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        int systemType = getSystemType();
        UserAuthor userAuthor =null;
        if (SystemType.ADMIN.getValue() == getSystemType()) {
            userAuthor = (UserAuthor) request.getSession().getAttribute(AdminAppConst.USER_AUTHOR);
        } else if (SystemType.CLIENT.getValue() == systemType) {
            userAuthor = (UserAuthor) request.getSession().getAttribute(ClientAppConst.USER_AUTHOR);
        } else if (SystemType.WEIXIN.getValue() == systemType) {
            userAuthor= (UserAuthor) request.getSession().getAttribute(WxAppConst.USER_AUTHOR);
        }
        return userAuthor;
    }

    /**
     * 获取注册信息
     * @return
     */
    public static UserRegister getUserInfo() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        int systemType = getSystemType();
        UserRegister userRegister =null;
        if (SystemType.ADMIN.getValue() == getSystemType()) {
            userRegister = (UserRegister) request.getSession().getAttribute(AdminAppConst.USER_INFO);
        } else if (SystemType.CLIENT.getValue() == systemType) {
            userRegister = (UserRegister) request.getSession().getAttribute(ClientAppConst.USER_INFO);
        } else if (SystemType.WEIXIN.getValue() == systemType) {
            userRegister= (UserRegister) request.getSession().getAttribute(WxAppConst.USER_INFO);
        }
        return userRegister;
    }

    /**
     * 清除会话信息
     * @return
     */
    public static void clearSession(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        int systemType = getSystemType();
        if (SystemType.ADMIN.getValue() == getSystemType()) {
            request.getSession().removeAttribute(AdminAppConst.USER_INFO);
        } else if (SystemType.CLIENT.getValue() == systemType) {
            request.getSession().removeAttribute(ClientAppConst.USER_INFO);
        } else if (SystemType.WEIXIN.getValue() == systemType) {
            request.getSession().removeAttribute(WxAppConst.USER_INFO);
        }
    }

    //获取当前类型
    public static int getSystemType(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = request.getSession();
        int system_type = 0;
        if(ObjectUtils.isNotNull(session.getAttribute("system_type"))){
            system_type = (int) session.getAttribute("system_type");
        }
        return system_type;
    }
    public static void setSystemType(int systemType){//1:pc 2.client 3.weixin
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = request.getSession();
        session.setAttribute("system_type",systemType);
    }

    //设置其他对象
    public static void setAttribute(String key,Object value){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = request.getSession();
        session.setAttribute(key,value);
    }
    public static <T> T getAttribute(String key){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = request.getSession();
        return (T) session.getAttribute(key);
    }

}
